package cn.exrick.controller;

import cn.exrick.common.exception.NeedLoginException;
import cn.exrick.common.exception.XmallException;
import cn.exrick.pojo.TbResource;
import cn.exrick.pojo.TbResourceCat;
import cn.exrick.pojo.common.Result;
import cn.exrick.pojo.front.Member;
import cn.exrick.pojo.front.ResourceResult;
import cn.exrick.service.LoginService;
import cn.exrick.service.ResourceCatService;
import cn.exrick.service.ResourceService;
import cn.exrick.utils.ResultUtil;
import cn.exrick.utils.mybatis.Page;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 资源中心相关接口
 *
 * @author
 * @date
 */
@RestController
@Api(description= "资源中心")
@RequestMapping("/api/resource")
public class ResourceFrontController {

    @Autowired
    private ResourceCatService resourceCatService;
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取资源列表")
    public Result<Page<ResourceResult>> getOrderList(String token, @RequestParam(defaultValue = "0") int page,
                                                     @RequestParam(defaultValue = "5") int size, Long category, String search) {
        page++;
        /*Member member = loginService.getUserByToken(token);
        if (member == null) {
            throw new NeedLoginException();
        }*/
        Page<ResourceResult> pageOrder = resourceService.getResourcePageForApi(page, size, category, search);
        return new ResultUtil<Page<ResourceResult>>().setData(pageOrder);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "获取资源详情")
    public Result<ResourceResult> getQuestionDetail(Long resourceId) {
        if(null == resourceId){
            throw new XmallException("资源ID不能为空");
        }
        ResourceResult result = resourceService.getDetailForApi(resourceId);
        if(null == result){
            throw new XmallException("资源不存在");
        }
        return new ResultUtil<ResourceResult>().setData(result);
    }

    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    @ApiOperation(value = "发布资源")
    public Result<Object> save(String token, String content, Long category, String image){
        Member member = loginService.getUserByToken(token);
        if (member == null) {
            throw new NeedLoginException();
        }
        if(StrUtil.isBlank(content)){
            throw new XmallException("资源内容不能为空");
        }
        if(null == category){
            throw new XmallException("资源类型不能为空");
        }

        TbResource tbResource = new TbResource();
        tbResource.setContent(content);
        tbResource.setCatId(category);
        tbResource.setImage(image);
        tbResource.setMemberId(member.getId());
        tbResource.setCreated(new Date());

        resourceService.save(tbResource);

        return new ResultUtil<Object>().setData(true);
    }

    @RequestMapping(value = "/cat/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取所有资源分类")
    public Result<List<TbResourceCat>> getAll(){
        List<TbResourceCat> list = resourceCatService.getAll();
        return new ResultUtil<List<TbResourceCat>>().setData(list);
    }


}
